home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / Tickle-4.0 (tcl) / tcl / tests / exec.test < prev    next >
Encoding:
Text File  |  1993-11-02  |  14.8 KB  |  443 lines  |  [TEXT/MPS ]

  1. # Commands covered:  exec
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1993 The Regents of the University of California.
  8. # All rights reserved.
  9. #
  10. # Permission is hereby granted, without written agreement and without
  11. # license or royalty fees, to use, copy, modify, and distribute this
  12. # software and its documentation for any purpose, provided that the
  13. # above copyright notice and the following two paragraphs appear in
  14. # all copies of this software.
  15. #
  16. # IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  17. # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  18. # OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  19. # CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. #
  21. # THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  22. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  23. # AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  24. # ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  25. # PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  26. #
  27. # $Header: /user6/ouster/tcl/tests/RCS/exec.test,v 1.30 93/09/16 16:57:43 ouster Exp $ (Berkeley)
  28.  
  29. if {[string compare test [info procs test]] == 1} then {source defs}
  30.  
  31. if {[info exists MACINTOSH]} {
  32.     puts stdout ""
  33.     puts stdout "exec is unimplemented on the Macintosh."
  34.     puts stdout ""
  35.     return ""
  36. }
  37.  
  38. # Basic operations.
  39.  
  40. test exec-1.1 {basic exec operation} {
  41.     exec echo a b c
  42. } "a b c"
  43. test exec-1.2 {pipelining} {
  44.     exec echo a b c d | cat | cat
  45. } "a b c d"
  46. test exec-1.3 {pipelining} {
  47.     set a [exec echo a b c d | cat | wc]
  48.     list [scan $a "%d %d %d" b c d] $b $c $d
  49. } {3 1 4 8}
  50.  
  51. # I/O redirection: input from Tcl command.
  52.  
  53. test exec-2.1 {redirecting input from immediate source} {
  54.     exec cat << "Sample text"
  55. } {Sample text}
  56. test exec-2.2 {redirecting input from immediate source} {
  57.     exec << "Sample text" cat | cat
  58. } {Sample text}
  59. test exec-2.3 {redirecting input from immediate source} {
  60.     exec cat << "Sample text" | cat
  61. } {Sample text}
  62. test exec-2.4 {redirecting input from immediate source} {
  63.     exec  cat | cat << "Sample text"
  64. } {Sample text}
  65. test exec-2.5 {redirecting input from immediate source} {
  66.     exec cat "<<Joined to arrows"
  67. } {Joined to arrows}
  68.  
  69. # I/O redirection: output to file.
  70.  
  71. catch {exec rm -f gorp.file}
  72. test exec-3.1 {redirecting output to file} {
  73.     exec echo "Some simple words" > gorp.file
  74.     exec cat gorp.file
  75. } "Some simple words"
  76. test exec-3.2 {redirecting output to file} {
  77.     exec echo "More simple words" | >gorp.file cat | cat
  78.     exec cat gorp.file
  79. } "More simple words"
  80. test exec-3.3 {redirecting output to file} {
  81.     exec > gorp.file echo "Different simple words" | cat | cat
  82.     exec cat gorp.file
  83. } "Different simple words"
  84. test exec-3.4 {redirecting output to file} {
  85.     exec echo "Some simple words" >gorp.file
  86.     exec cat gorp.file
  87. } "Some simple words"
  88. test exec-3.5 {redirecting output to file} {
  89.     exec echo "First line" >gorp.file
  90.     exec echo "Second line" >> gorp.file
  91.     exec cat gorp.file
  92. } "First line\nSecond line"
  93. test exec-3.6 {redirecting output to file} {
  94.     exec echo "First line" >gorp.file
  95.     exec echo "Second line" >>gorp.file
  96.     exec cat gorp.file
  97. } "First line\nSecond line"
  98. test exec-3.7 {redirecting output to file} {
  99.     set f [open gorp.file w]
  100.     puts $f "Line 1"
  101.     flush $f
  102.     exec echo "More text" >@ $f
  103.     exec echo >@$f "Even more"
  104.     puts $f "Line 3"
  105.     close $f
  106.     exec cat gorp.file
  107. } "Line 1\nMore text\nEven more\nLine 3"
  108.  
  109. # I/O redirection: output and stderr to file.
  110.  
  111. catch {exec rm -f gorp.file}
  112. test exec-4.1 {redirecting output and stderr to file} {
  113.     exec echo "test output" >& gorp.file
  114.     exec cat gorp.file
  115. } "test output"
  116. test exec-4.2 {redirecting output and stderr to file} {
  117.     list [exec sh -c "echo foo bar 1>&2" >&gorp.file] \
  118.         [exec cat gorp.file]
  119. } {{} {foo bar}}
  120. test exec-4.3 {redirecting output and stderr to file} {
  121.     exec echo "first line" > gorp.file
  122.     list [exec sh -c "echo foo bar 1>&2" >>&gorp.file] \
  123.         [exec cat gorp.file]
  124. } "{} {first line\nfoo bar}"
  125. test exec-4.4 {redirecting output and stderr to file} {
  126.     set f [open gorp.file w]
  127.     puts $f "Line 1"
  128.     flush $f
  129.     exec echo "More text" >&@ $f
  130.     exec echo >&@$f "Even more"
  131.     puts $f "Line 3"
  132.     close $f
  133.     exec cat gorp.file
  134. } "Line 1\nMore text\nEven more\nLine 3"
  135. test exec-4.5 {redirecting output and stderr to file} {
  136.     set f [open gorp.file w]
  137.     puts $f "Line 1"
  138.     flush $f
  139.     exec >&@ $f sh -c "echo foo bar 1>&2"
  140.     exec >&@$f sh -c "echo xyzzy 1>&2"
  141.     puts $f "Line 3"
  142.     close $f
  143.     exec cat gorp.file
  144. } "Line 1\nfoo bar\nxyzzy\nLine 3"
  145.  
  146. # I/O redirection: input from file.
  147.  
  148. exec echo "Just a few thoughts" > gorp.file
  149. test exec-5.1 {redirecting input from file} {
  150.     exec cat < gorp.file
  151. } {Just a few thoughts}
  152. test exec-5.2 {redirecting input from file} {
  153.     exec cat | cat < gorp.file
  154. } {Just a few thoughts}
  155. test exec-5.3 {redirecting input from file} {
  156.     exec cat < gorp.file | cat
  157. } {Just a few thoughts}
  158. test exec-5.4 {redirecting input from file} {
  159.     exec < gorp.file cat | cat
  160. } {Just a few thoughts}
  161. test exec-5.5 {redirecting input from file} {
  162.     exec cat <gorp.file
  163. } {Just a few thoughts}
  164. test exec-5.6 {redirecting input from file} {
  165.     set f [open gorp.file r]
  166.     set result [exec cat <@ $f]
  167.     close $f
  168.     set result
  169. } {Just a few thoughts}
  170. test exec-5.7 {redirecting input from file} {
  171.     set f [open gorp.file r]
  172.     set result [exec <@$f cat]
  173.     close $f
  174.     set result
  175. } {Just a few thoughts}
  176.  
  177. # I/O redirection: standard error through a pipeline.
  178.  
  179. test exec-6.1 {redirecting stderr through a pipeline} {
  180.     exec sh -c "echo foo bar" |& cat
  181. } "foo bar"
  182. test exec-6.2 {redirecting stderr through a pipeline} {
  183.     exec sh -c "echo foo bar 1>&2" |& cat
  184. } "foo bar"
  185. test exec-6.3 {redirecting stderr through a pipeline} {
  186.     exec sh -c "echo foo bar 1>&2" |& sh -c "echo second msg 1>& 2; cat" |& cat
  187. } "second msg\nfoo bar"
  188.  
  189. # I/O redirection: combinations.
  190.  
  191. catch {exec rm -f gorp.file2}
  192. test exec-7.1 {multiple I/O redirections} {
  193.     exec << "command input" > gorp.file2 cat < gorp.file
  194.     exec cat gorp.file2
  195. } {Just a few thoughts}
  196. test exec-7.2 {multiple I/O redirections} {
  197.     exec < gorp.file << "command input" cat
  198. } {command input}
  199.  
  200. # Long input to command and output from command.
  201.  
  202. set a "0123456789 xxxxxxxxx abcdefghi ABCDEFGHIJK\n"
  203. set a [concat $a $a $a $a]
  204. set a [concat $a $a $a $a]
  205. set a [concat $a $a $a $a]
  206. set a [concat $a $a $a $a]
  207. test exec-8.1 {long input and output} {
  208.     exec cat << $a
  209. } $a
  210.  
  211. # Commands that return errors.
  212.  
  213. test exec-9.1 {commands returning errors} {
  214.     set x [catch {exec gorp456} msg]
  215.     list $x $msg [lindex $errorCode 0] [lrange $errorCode 2 end]
  216. } {1 {couldn't find "gorp456" to execute} CHILDSTATUS 1}
  217. test exec-9.2 {commands returning errors} {
  218.     set x [catch {exec foo123 | gorp456} msg]
  219.     set x1 {couldn't find "foo123" to execute
  220. couldn't find "gorp456" to execute}
  221.     set x2 {couldn't find "gorp456" to execute
  222. couldn't find "foo123" to execute}
  223.     set y [expr {($msg == $x1) || ($msg == $x2)}]
  224.     list $x $y [lindex $errorCode 0] [lrange $errorCode 2 end]
  225. } {1 1 CHILDSTATUS 1}
  226. test exec-9.3 {commands returning errors} {
  227.     list [catch {exec sleep 1 | sh -c "exit 43" | sleep 1} msg] $msg
  228. } {1 {child process exited abnormally}}
  229. test exec-9.4 {commands returning errors} {
  230.     list [catch {exec gorp456 | echo a b c} msg] $msg
  231. } {1 {a b c
  232. couldn't find "gorp456" to execute}}
  233. test exec-9.5 {commands returning errors} {
  234.     list [catch {exec sh -c "echo error msg 1>&2"} msg] $msg
  235. } {1 {error msg}}
  236. test exec-9.6 {commands returning errors} {
  237.     list [catch {exec sh -c "echo error msg 1>&2" | sh -c "echo error msg 1>&2"} msg] $msg
  238. } {1 {error msg
  239. error msg}}
  240.  
  241. # Errors in executing the Tcl command, as opposed to errors in the
  242. # processes that are invoked.
  243.  
  244. test exec-10.1 {errors in exec invocation} {
  245.     list [catch {exec} msg] $msg
  246. } {1 {wrong # args: should be "exec ?switches? arg ?arg ...?"}}
  247. test exec-10.2 {errors in exec invocation} {
  248.     list [catch {exec | cat} msg] $msg
  249. } {1 {illegal use of | or |& in command}}
  250. test exec-10.3 {errors in exec invocation} {
  251.     list [catch {exec cat |} msg] $msg
  252. } {1 {illegal use of | or |& in command}}
  253. test exec-10.4 {errors in exec invocation} {
  254.     list [catch {exec cat | | cat} msg] $msg
  255. } {1 {illegal use of | or |& in command}}
  256. test exec-10.5 {errors in exec invocation} {
  257.     list [catch {exec cat | |& cat} msg] $msg
  258. } {1 {illegal use of | or |& in command}}
  259. test exec-10.6 {errors in exec invocation} {
  260.     list [catch {exec cat |&} msg] $msg
  261. } {1 {illegal use of | or |& in command}}
  262. test exec-10.7 {errors in exec invocation} {
  263.     list [catch {exec cat <} msg] $msg
  264. } {1 {can't specify "<" as last word in command}}
  265. test exec-10.8 {errors in exec invocation} {
  266.     list [catch {exec cat >} msg] $msg
  267. } {1 {can't specify ">" as last word in command}}
  268. test exec-10.9 {errors in exec invocation} {
  269.     list [catch {exec cat <<} msg] $msg
  270. } {1 {can't specify "<<" as last word in command}}
  271. test exec-10.10 {errors in exec invocation} {
  272.     list [catch {exec cat >>} msg] $msg
  273. } {1 {can't specify ">>" as last word in command}}
  274. test exec-10.11 {errors in exec invocation} {
  275.     list [catch {exec cat >&} msg] $msg
  276. } {1 {can't specify ">&" as last word in command}}
  277. test exec-10.12 {errors in exec invocation} {
  278.     list [catch {exec cat >>&} msg] $msg
  279. } {1 {can't specify ">>&" as last word in command}}
  280. test exec-10.13 {errors in exec invocation} {
  281.     list [catch {exec cat >@} msg] $msg
  282. } {1 {can't specify ">@" as last word in command}}
  283. test exec-10.14 {errors in exec invocation} {
  284.     list [catch {exec cat <@} msg] $msg
  285. } {1 {can't specify "<@" as last word in command}}
  286. test exec-10.15 {errors in exec invocation} {
  287.     list [catch {exec cat < a/b/c} msg] [string tolower $msg]
  288. } {1 {couldn't read file "a/b/c": no such file or directory}}
  289. test exec-10.16 {errors in exec invocation} {
  290.     list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
  291. } {1 {couldn't write file "a/b/c": no such file or directory}}
  292. test exec-10.17 {errors in exec invocation} {
  293.     list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
  294. } {1 {couldn't write file "a/b/c": no such file or directory}}
  295. set f [open gorp.file w]
  296. test exec-10.18 {errors in exec invocation} {
  297.     list [catch {exec cat <@ $f} msg] $msg
  298. } "1 {\"$f\" wasn't opened for reading}"
  299. close $f
  300. set f [open gorp.file r]
  301. test exec-10.19 {errors in exec invocation} {
  302.     list [catch {exec cat >@ $f} msg] $msg
  303. } "1 {\"$f\" wasn't opened for writing}"
  304. close $f
  305.  
  306. # Commands in background.
  307.  
  308. test exec-11.1 {commands in background} {
  309.     set x [lindex [time {exec sleep 2 &}] 0]
  310.     expr $x<1000000
  311. } 1
  312. test exec-11.2 {commands in background} {
  313.     list [catch {exec echo a &b} msg] $msg
  314. } {0 {a &b}}
  315. test exec-11.3 {commands in background} {
  316.     llength [exec sleep 1 &]
  317. } 1
  318. test exec-11.4 {commands in background} {
  319.     llength [exec sleep 1 | sleep 1 | sleep 1 &]
  320. } 3
  321.  
  322. # Make sure that background commands are properly reaped when
  323. # they eventually die.
  324.  
  325. exec sleep 3
  326. if $atBerkeley {
  327.     test exec-12.1 {reaping background processes} {
  328.     for {set i 0} {$i < 20} {incr i} {
  329.         exec echo foo > /dev/null &
  330.     }
  331.     exec sleep 1
  332.     catch {exec ps | fgrep "echo foo" | fgrep -v fgrep | wc} msg
  333.     lindex $msg 0
  334.     } 0
  335.     test exec-12.2 {reaping background processes} {
  336.     exec sleep 2 | sleep 2 | sleep 2 &
  337.     catch {exec ps | fgrep "sleep 2" | fgrep -v fgrep | wc} msg
  338.     set x [lindex $msg 0]
  339.     exec sleep 3
  340.     catch {exec ps | fgrep "sleep 2" | fgrep -v fgrep | wc} msg
  341.     list $x [lindex $msg 0]
  342.     } {3 0}
  343.     test exec-12.3 {reaping background processes} {
  344.     exec sleep 1000 &
  345.     exec sleep 1000 &
  346.     set x [exec ps | fgrep "sleep 1000" | fgrep -v fgrep]
  347.     set pids {}
  348.     foreach i [split $x \n] {
  349.         lappend pids [lindex $i 0]
  350.     }
  351.     foreach i $pids {
  352.         catch {exec kill -STOP $i}
  353.     }
  354.     catch {exec ps | fgrep "sleep 1000" | fgrep -v fgrep | wc} msg
  355.     set x [lindex $msg 0]
  356.     
  357.     foreach i $pids {
  358.         catch {exec kill -KILL $i}
  359.     }
  360.     catch {exec ps | fgrep "sleep 1000" | fgrep -v fgrep | wc} msg
  361.     list $x [lindex $msg 0]
  362.     } {2 0}
  363. }
  364.  
  365. # Make sure "errorCode" is set correctly.
  366.  
  367. test exec-13.1 {setting errorCode variable} {
  368.     list [catch {exec cat < a/b/c} msg] [string tolower $errorCode]
  369. } {1 {posix enoent {no such file or directory}}}
  370. test exec-13.2 {setting errorCode variable} {
  371.     list [catch {exec cat > a/b/c} msg] [string tolower $errorCode]
  372. } {1 {posix enoent {no such file or directory}}}
  373. test exec-13.3 {setting errorCode variable} {
  374.     set x [catch {exec _weirdo_command_} msg]
  375.     list $x $msg [lindex $errorCode 0] [lrange $errorCode 2 end]
  376. } {1 {couldn't find "_weirdo_command_" to execute} CHILDSTATUS 1}
  377.  
  378. # Switches before the first argument
  379.  
  380. test exec-14.1 {-keepnewline switch} {
  381.     exec -keepnewline echo foo
  382. } "foo\n"
  383. test exec-14.2 {-keepnewline switch} {
  384.     list [catch {exec -keepnewline} msg] $msg
  385. } {1 {wrong # args: should be "exec ?switches? arg ?arg ...?"}}
  386. test exec-14.3 {unknown switch} {
  387.     list [catch {exec -gorp} msg] $msg
  388. } {1 {bad switch "-gorp": must be -keepnewline or --}}
  389. test exec-14.4 {-- switch} {
  390.     list [catch {exec -- -gorp} msg] $msg
  391. } {1 {couldn't find "-gorp" to execute}}
  392.  
  393. # Redirecting standard error separately from standard output
  394.  
  395. test exec-15.1 {standard error redirection} {
  396.     exec echo "First line" > gorp.file
  397.     list [exec sh -c "echo foo bar 1>&2" 2> gorp.file] \
  398.         [exec cat gorp.file]
  399. } {{} {foo bar}}
  400. test exec-15.2 {standard error redirection} {
  401.     list [exec sh -c "echo foo bar 1>&2" | echo biz baz >gorp.file \
  402.         2> gorp.file2] [exec cat gorp.file] \
  403.         [exec cat gorp.file2]
  404. } {{} {biz baz} {foo bar}}
  405. test exec-15.3 {standard error redirection} {
  406.     list [exec sh -c "echo foo bar 1>&2" | echo biz baz 2>gorp.file \
  407.         > gorp.file2] [exec cat gorp.file] \
  408.         [exec cat gorp.file2]
  409. } {{} {foo bar} {biz baz}}
  410. test exec-15.4 {standard error redirection} {
  411.     set f [open gorp.file w]
  412.     puts $f "Line 1"
  413.     flush $f
  414.     exec sh -c "echo foo bar 1>&2" 2>@ $f
  415.     puts $f "Line 3"
  416.     close $f
  417.     exec cat gorp.file
  418. } {Line 1
  419. foo bar
  420. Line 3}
  421. test exec-15.5 {standard error redirection} {
  422.     exec echo "First line" > gorp.file
  423.     exec sh -c "echo foo bar 1>&2" 2>> gorp.file
  424.     exec cat gorp.file
  425. } {First line
  426. foo bar}
  427. test exec-15.6 {standard error redirection} {
  428.     exec sh -c "echo foo bar 1>&2" > gorp.file2 2> gorp.file \
  429.         >& gorp.file 2> gorp.file2 | echo biz baz
  430.     list [exec cat gorp.file] [exec cat gorp.file2]
  431. } {{biz baz} {foo bar}}
  432.  
  433. if $atBerkeley {
  434.     test exec-16.1 {restore signal settings before exec} {
  435.     set f [open {|cat exec.test} r]
  436.     list [catch {close $f} msg] [string tolower $msg]
  437.     } {1 {child killed: write on pipe with no readers}}
  438. }
  439.  
  440. catch {exec rm -f gorp.file}
  441. catch {exec rm -f gorp.file2}
  442. return {}
  443.